package com.ych.modules.common.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ych.modules.base.dbEntity.TUserRaffleTicketEntity;
import com.ych.modules.base.service.ITUserRaffleTicketService;
import com.ych.modules.common.entity.query.QueryUserRaffleTicketEntity;
import com.ych.modules.common.service.UserRaffleTicketService;
import com.ych.utils.beanToMap.BeanMapUtil;
import com.ych.utils.mysqlPageUtil.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/1/2
 * Time: 11:44
 * Description:
 */
@Service("UserRaffleTicketService")
public class UserRaffleTicketServiceImpl implements UserRaffleTicketService {
    @Autowired
    private ITUserRaffleTicketService itUserRaffleTicketService;

    @Override
    public TUserRaffleTicketEntity getRaffleTicket(String ticketId) {
        TUserRaffleTicketEntity ticketEntity = itUserRaffleTicketService.selectById(ticketId);
        if (null == ticketEntity) {
            throw new RuntimeException("抽奖券不存在！");
        }
        return ticketEntity;
    }

    @Override
    public Page<TUserRaffleTicketEntity> listRaffleTicket(QueryUserRaffleTicketEntity entity) {
        Wrapper<TUserRaffleTicketEntity> wrapper = new EntityWrapper<TUserRaffleTicketEntity>()
                .where("1=1")
                .eq(StringUtils.isNotBlank(entity.getEventId()), "event_id", entity.getEventId())
                .eq(StringUtils.isNotBlank(entity.getIsChecked()), "is_checked", entity.getIsChecked())
                .eq(StringUtils.isNotBlank(entity.getRedeemCode()), "redeem_code", entity.getRedeemCode())
                .eq(StringUtils.isNotBlank(entity.getUserId()), "user_id", entity.getUserId())
                .orderBy("create_time desc");
        Page<TUserRaffleTicketEntity> page = itUserRaffleTicketService.selectPage(new Query<TUserRaffleTicketEntity>(BeanMapUtil.toPageQuery(entity)).getPage(), wrapper);
        return page;
    }
}
